var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        inited : true,
        region : [],
        address : {regionarr:[]},
        params : {},
        street : '',
        lat : '',
        lng : '',
    },  
    onLoad : function(options){
        var self = this;
        app.com.auth(function(user){
            
            app.com.http('user_v1','POST',{op:'getadd',id:options.id},0,true,'','',function(res){
                if (res.data && !res.data.errno) {
                    self.setData({
                        address : res.data.data.address,
                        params : res.data.data.params,
                    });

                }
            });
            
        });
        
        app.com.getLocation(self,function(loc){
            self.setData({
                lng : loc.longitude,
                lat : loc.latitude,
            });
        })

        wx.setNavigationBarTitle({
          title:  '添加地址',
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: '添加地址',
            path: '',
        }
    },
    address(){
        var self = this;
        wx.chooseLocation({
            success : function(res){
                self.setData({
                    'address.lat' : res.latitude,
                    'address.lng' : res.longitude,
                    street : res.name,
                    'address.street' : res.name,
                });
            }
        });
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },
    showadd : function(){
        this.setData({
            actid : 0,
            showadd : !this.data.showadd,
        });
    },
    inputZone : function(e){
        this.setData({
            'address.regionarr' : e.detail.value,
            'address.region' : e.detail.value[0]+','+e.detail.value[1]+','+e.detail.value[2],
            'address.province' : e.detail.value[0],
            'address.city' : e.detail.value[1],
            'address.country' : e.detail.value[2],
            'address.pshort' : e.detail.value[3] ? e.detail.value[3] : '',
            'address.code' : e.detail.value[4] ? e.detail.value[4] : '',
        });
    },
    inputReceiver  : function( e ){
        var value = e.detail.value;
        this.setData({
            'address.name' : value,
        });
    },
    inputPhone  : function( e ){
        var value = e.detail.value;
        this.setData({
            'address.tel' : value,
        });
    },
    inputAddress  : function( e ){
        var value = e.detail.value;
        this.setData({
            'address.street' : value,
        });
    },
    inputDefault  : function( e ){
        var value = e.detail.value;
        this.setData({
            'address.isact' : value ? 1 : 0,
        });
    },
    selectarea(){
        this.selectComponent('#ausarea').show();
    },
    sub : function(){
        var self = this;
        var postdata = self.data.address;
        postdata.op = 'addaddress';
        postdata.v = 1;
        if(!postdata.lat){
            postdata.lat = self.data.lat > 0 ? self.data.lat : '';
        }
        if(!postdata.lng){
            postdata.lng = self.data.lng > 0 ? self.data.lng : '';
        }

        if( !app.com.verify('mobile',postdata.tel) ){
           app.com.alert('手机号码格式不对');return false;
        }
        app.com.http('user_v1','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno) {
                self.setData({
                    address : {regionarr:[]},
                });
                app.com.toast(res.data.message);
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },

})